<?php

App::uses('Crud', 'Model');
App::uses('Certification', 'Model');
App::uses('CertificationDetail', 'Model');
App::uses('Workstation', 'Model');
App::uses('WorkstationConfig', 'Model');
App::uses('User', 'Model');
App::uses('UserLine', 'Model');
App::uses('Validation', 'Utility');

class TeamUser extends Crud
{

    const STATUS_DISABLED = 0;
    const STATUS_ENABLED = 1;
    const TYPE_NORMAL = 1;
    const TYPE_LEADER = 2;

    static public function getTypes()
    {
        return array(
            array('text' => 'Asociado', 'value' => self::TYPE_NORMAL),
            array('text' => 'Preparador', 'value' => self::TYPE_LEADER),
        );
    }

    /**
     * Obtenemos los integrantes de un equipo
     * @param string $teamId
     */
    public function getTeamWithDays($teamId, $days)
    {
        $filters = array(
            'joins' => array(
                array(
                    'table' => 'teams',
                    'alias' => 't',
                    'type' => 'INNER',
                    'conditions' => array(
                        't.id = TeamUser.team_id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'User',
                    'type' => 'INNER',
                    'conditions' => array(
                        'User.id = TeamUser.user_id',
                        'TeamUser.status' => self::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'user_lines',
                    'alias' => 'ul',
                    'type' => 'left',
                    'conditions' => array(
                        'ul.line_id = t.line_id',
                        'ul.user_id = User.id',
                        'ul.status' => UserLine::STATUS_ENABLED
                    ),
                ),
            ),
            'conditions' => array(
                'TeamUser.team_id' => $teamId,
                'ul.days >=' => $days,
            ),
        );
        $numRecords = $this->find('count', $filters);
        return $numRecords;
    }

    public function getTeam($teamId) {
        $filters = array(
            'fields' => array(
                'TeamUser.id',
                'User.id as user_id',
                'User.admission_date',
                'User.full_name',
                'User.curp',
                'User.nomina',
                'IFNULL(ul.days, 0) as days',
            ),
            'joins' => array(
                array(
                    'table' => 'teams',
                    'alias' => 't',
                    'type' => 'INNER',
                    'conditions' => array(
                        't.id = TeamUser.team_id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'User',
                    'type' => 'INNER',
                    'conditions' => array(
                        'User.id = TeamUser.user_id',
                    ),
                ),
                array(
                    'table' => 'user_lines',
                    'alias' => 'ul',
                    'type' => 'left',
                    'conditions' => array(
                        'ul.line_id = t.line_id',
                        'ul.user_id = User.id',
                        'ul.status' => UserLine::STATUS_ENABLED
                    ),
                ),
            ),
            'conditions' => array(
                'TeamUser.team_id' => $teamId,
                'TeamUser.status' => self::STATUS_ENABLED,
            ),
        );
        $records = $this->flatArray($this->find('all', $filters));
        return $records;
    }

    public function getTeamWithDetails($teamId)
    {
        $filters = array(
            'fields' => array(
                'TeamUser.id',
                'User.id as user_id',
                'User.admission_date',
                'User.full_name',
                'User.curp',
                'User.nomina',
                'IFNULL(ul.days, 0) as days',
                'cat.id as level',
                'cat.name as level_name',
                'cat.icon as level_icon',
                'cat.colour as level_colour',
            ),
            'joins' => array(
                array(
                    'table' => 'teams',
                    'alias' => 't',
                    'type' => 'INNER',
                    'conditions' => array(
                        't.id = TeamUser.team_id',
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'User',
                    'type' => 'INNER',
                    'conditions' => array(
                        'User.id = TeamUser.user_id',
                    ),
                ),
                array(
                    'table' => 'user_lines',
                    'alias' => 'ul',
                    'type' => 'left',
                    'conditions' => array(
                        'ul.line_id = t.line_id',
                        'ul.user_id = User.id',
                        'ul.status' => UserLine::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'categorizations',
                    'alias' => 'cat',
                    'type' => 'inner',
                    'conditions' => array(
                        'cat.id = ul.level',
                    ),
                ),
            ),
            'conditions' => array(
                'TeamUser.team_id' => $teamId,
                'TeamUser.status' => self::STATUS_ENABLED,
            ),
        );
        $records = $this->flatArray($this->find('all', $filters));
        return $records;
    }

    public function deleteMember($teamId, $userId = null) {
        $filters = array(
            'fields' => array(
                'id',
                '"' . self::STATUS_DISABLED . '" as status',
            ),
            'conditions' => array(
                'team_id' => $teamId,
                'status' => self::STATUS_ENABLED,
            ),
        );
        if ($userId !== null) {
            $filters['conditions']['user_id'] = $userId;
        }
        $records = $this->flatArray($this->find('all', $filters));
        $this->saveAll($records, array(
            'fieldList' => array(
                'UserTeam' => array('status'),
        )));
    }

    public function getResumeByTeamAndLineAndUser($teamId, $lineId, $userId) {
        $filters = array(
            'fields' => array(
                'u.id as uId',
                'u.full_name as uFullName',
                'u.curp as uCurp',
                'u.nomina as uNomina',
                'wc.name as wcName',
                'ul.days as days',
                '"' . $lineId . '" as lId',
                'COUNT(IF(cd.qualification>=wc.minimum_qualification,1,null)) as ok',
                'COUNT(IF(cd.qualification<wc.minimum_qualification,1,null)) as nok',
                'COUNT(IF(c.success,1,null)) as c',
                'COUNT(IF(c.training,1,null)) as t',
            ),
            'joins' => array(
                array(
                    'table' => 'user_lines',
                    'alias' => 'ul',
                    'type' => 'INNER',
                    'conditions' => array(
                        'ul.user_id' => $userId,
                        'ul.line_id' => $lineId,
                    ),
                ),
                array(
                    'table' => 'certifications',
                    'alias' => 'c',
                    'type' => 'INNER',
                    'conditions' => array(
                        'TeamUser.user_id = c.user_id',
                        'c.status' => Certification::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'INNER',
                    'conditions' => array(
                        'w.id = c.workstation_id',
                        'w.status' => Workstation::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'certifications_details',
                    'alias' => 'cd',
                    'type' => 'INNER',
                    'conditions' => array(
                        'cd.certification_id = c.id',
                        'cd.status' => CertificationDetail::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'workstations_configs',
                    'alias' => 'wc',
                    'type' => 'INNER',
                    'conditions' => array(
                        'wc.id = cd.workstation_config_id',
                        'wc.status' => WorkstationConfig::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'c.user_id = u.id',
                        'u.status' => User::STATUS_ENABLED
                    ),
                ),
            ),
            'conditions' => array(
                'w.line_id' => $lineId,
                'OR' => array(
                    'c.training' => Certification::TRAINING_OK,
                    'c.success ' => Certification::SUCCESS_OK,
                ),
            ),
            'group' => array(
                'u.id',
                'u.full_name',
                'u.curp',
                'u.nomina',
                'wc.name',
                'wc.orden',
            ),
            'order' => array(
                'u.full_name ASC',
                'wc.orden ASC',
            ),
        );
        if (Validation::uuid($teamId) === true) {
            $filters['conditions']['TeamUser.team_id'] = $teamId;
        }
        if ($userId !== null) {
            $filters['conditions']['TeamUser.user_id'] = $userId;
        }
        return $this->flatArray($this->find('all', $filters));
    }

    public function getUsersByTeam($teamId) {
        $filters = array(
            'fields' => array(
                'u.full_name as name'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u.id = TeamUser.user_id'
                    ),
                ),
            ),
            'conditions' => array(
                'TeamUser.team_id' => $teamId,
                'TeamUser.status' => self::STATUS_ENABLED
            )
        );
        return $this->flatArray($this->find('all', $filters));
    }

    public function getUsersNominasByTeam($teamId) {
        $filters = array(
            'fields' => array(
                'u.nomina as nomina'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u.id = TeamUser.user_id'
                    ),
                ),
            ),
            'conditions' => array(
                'TeamUser.team_id' => $teamId,
                'TeamUser.status' => self::STATUS_ENABLED
            )
        );

        return $this->find('all', $filters);
    }
    
    public function getActiveTeamUsers($teamId) {
        $filters = array(
            'fields' => array(
                'u.full_name',
                'u.curp',
                'u.admission_date',
                'TeamUser.id',
                'u.nomina',
                'TeamUser.user_id',
                'TeamUser.leader',
                'u.status'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'INNER',
                    'conditions' => array(
                        'u.id = TeamUser.user_id',
                        'u.status != 0'
                    ),
                ),
            ),
            'conditions' => array(
                'TeamUser.team_id' => $teamId,
                'TeamUser.status' => self::STATUS_ENABLED
            ),
            'order' => array(
                'u.status ASC',
                'u.nomina ASC'
            )
        );
        return $this->flatArray($this->find('all', $filters));
    }


}
